1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package bench.serial;
29
30 import bench.Benchmark;
31 import java.io.ObjectInputStream;
32 import java.io.ObjectOutputStream;
33 import java.io.Serializable;
34 import java.lang.reflect.Constructor;
35 import java.lang.reflect.InvocationHandler;
36 import java.lang.reflect.Method;
37 import java.lang.reflect.Proxy;
38
39
40
41
42 public class ProxyArrays implements Benchmark {
43
44 static class DummyHandler implements InvocationHandler, Serializable {
45 public Object invoke(Object proxy, Method method, Object[] args)
46 throws Throwable
47 {
48 return null;
49 }
50 }
51
52 static interface DummyInterface {
53 public void foo();
54 }
55
56
57
58
59
60
61
62
63 public long run(String[] args) throws Exception {
64 int size = Integer.parseInt(args[0]);
65 int nbatches = Integer.parseInt(args[1]);
66 int ncycles = Integer.parseInt(args[2]);
67 Proxy[][] arrays = genArrays(size, ncycles);
68 StreamBuffer sbuf = new StreamBuffer();
69 ObjectOutputStream oout =
70 new ObjectOutputStream(sbuf.getOutputStream());
71 ObjectInputStream oin =
72 new ObjectInputStream(sbuf.getInputStream());
73
74 doReps(oout, oin, sbuf, arrays, 1);
75
76 long start = System.currentTimeMillis();
77 doReps(oout, oin, sbuf, arrays, nbatches);
78 return System.currentTimeMillis() - start;
79 }
80
81
82
83
84 Proxy[][] genArrays(int size, int narrays) throws Exception {
85 Class proxyClass =
86 Proxy.getProxyClass(DummyInterface.class.getClassLoader(),
87 new Class[] { DummyInterface.class });
88 Constructor proxyCons =
89 proxyClass.getConstructor(new Class[] { InvocationHandler.class });
90 Object[] consArgs = new Object[] { new DummyHandler() };
91 Proxy[][] arrays = new Proxy[narrays][size];
92 for (int i = 0; i < narrays; i++) {
93 for (int j = 0; j < size; j++) {
94 arrays[i][j] = (Proxy) proxyCons.newInstance(consArgs);
95 }
96 }
97 return arrays;
98 }
99
100
101
102
103
104 void doReps(ObjectOutputStream oout, ObjectInputStream oin,
105 StreamBuffer sbuf, Proxy[][] arrays, int nbatches)
106 throws Exception
107 {
108 int ncycles = arrays.length;
109 for (int i = 0; i < nbatches; i++) {
110 sbuf.reset();
111 oout.reset();
112 for (int j = 0; j < ncycles; j++) {
113 oout.writeObject(arrays[j]);
114 }
115 oout.flush();
116 for (int j = 0; j < ncycles; j++) {
117 oin.readObject();
118 }
119 }
120 }
121 }